package com.example.restatr.Atcivity.Traffic;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.Traffic.Bean.TrafficCheckCarOrderBean;
import com.example.restatr.Atcivity.Traffic.Bean.TrafficCheckCarPlaceBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class CheckCarListAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private ListView list;
    private FloatingActionButton fab;
    private LinearLayout Null;
    private ImageView nullIcon;
    private TextView nullTitle;
    private List<TrafficCheckCarOrderBean.RowsBean> rowsBeanList = new ArrayList<>();

    @SuppressLint("RestrictedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_check_car_list_at );

        tb = findViewById( R.id.toolbar);
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        list = findViewById( R.id.list );
        fab = findViewById( R.id.fab );
        Null = findViewById( R.id.Null );
        nullIcon = findViewById( R.id.null_icon );
        nullTitle = findViewById( R.id.null_title );
        Tools.SetBarTitle( tb, barTitle, "我的预约", search, this,false );
        fab.setVisibility( View.GONE );
        nullIcon.setImageResource( R.drawable.ic_baseline_not_interested_24 );
        nullTitle.setText( "暂无预约..." );
        GetCheckCarOrder();
    }

    private void GetCheckCarOrder() {
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/traffic/checkCar/apply/list", MainActivity.sp.getString( "token", "" ) );
            TrafficCheckCarOrderBean trafficCheckCarOrderBean = new Gson().fromJson( token, TrafficCheckCarOrderBean.class );

            rowsBeanList.addAll( trafficCheckCarOrderBean.getRows() );
            runOnUiThread( () -> {
                if (rowsBeanList.size() > 0) {
                    list.setVisibility( View.VISIBLE );
                    Null.setVisibility( View.GONE );
                    list.setAdapter( new adapter() );
                } else {
                    list.setVisibility( View.GONE );
                    Null.setVisibility( View.VISIBLE );
                }
            } );
        } ).start();
    }

    private void GetCheckCarInfo(int pos, TextView textView) {
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/traffic/checkCar/place/list?placeName=" + rowsBeanList.get( pos ).getPlaceName(), MainActivity.sp.getString( "token", "" ) );
            List<TrafficCheckCarPlaceBean.RowsBean> rows = new Gson().fromJson( token, TrafficCheckCarPlaceBean.class ).getRows();
            runOnUiThread( () -> {
                if (rows.size()>0){

                    Log.d( "TAG", "GetCheckCarInfo: "+rows.get(0 ).getAddress() );
                    textView.setText( rows.get(0 ).getAddress() );
                }
            } );
        } ).start();
    }

    class adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return rowsBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return rowsBeanList.get( position );
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate( R.layout.job_item_layout, null );
            TextView professionName = convertView.findViewById( R.id.professionName );
            TextView salary = convertView.findViewById( R.id.salary );
            TextView need = convertView.findViewById( R.id.need );
            TextView companyName = convertView.findViewById( R.id.companyName );
            TextView applyFor = convertView.findViewById( R.id.applyFor );

            professionName.setText( rowsBeanList.get( position ).getPlaceName() );
            salary.setText( rowsBeanList.get( position ).getAptTime() );
            need.setText( "预约用户：" + rowsBeanList.get( position ).getUserName() );
            GetCheckCarInfo( position, companyName );
            applyFor.setVisibility( View.GONE );
            return convertView;
        }
    }
}